client_list = [
    {'cardsid': '0001', 'name': 'liuchaojun', 'pasword': '123', 'deposit': '1000'},
    {'cardsid': '0002', 'name': 'liuchaojun1', 'pasword': '456', 'deposit': '2000'},
    {'cardsid': '0003', 'name': 'liuchaojun2', 'pasword': '789', 'deposit': '3000'},
]
# 是否还在登陆状态标志
login_key = True
# 登陆状态是否成功标志
success_key = False
# 获取列表中的索引
login_alive = 0


def login_in(cardid):
    '''
    根据卡号登陆系统
    :param cardid: cardsid 卡号
    :return: none
    '''
    global success_key, login_alive ,login_key
    success_key = False
    # 密码容错次数+1
    i = 4
    # 用来判断卡是否存在
    k = 0
    for kid, client in enumerate(client_list):
        # 判断卡是否在列表中
        if cardid != client['cardsid']:
            k += 1
        else:
            # True
            login_alive = kid
            password1 = client['pasword']
            while True:
                i -= 1
                password2 = input("您还有{}次机会,请重新输入您的密码或q退出:".format(i))
                if password2.upper() == 'Q':
                    print('您已取消登陆！')
                    break
                elif password1 != password2:
                    if i > 1:
                        print('密码错误!')
                        continue
                    else:
                        print('密码错误,您的卡已被锁定,请联系管理员!')
                        break
                else:
                    print('恭喜您{}登陆成功'.format(client_list[login_alive]['name']))
                    success_key = True
                    login_key = False
                    break
        if k == len(client_list):
            print('请输入正确的卡号!')
            continue



def login_out():
    '''
    退出系统
    :return: none
    '''
    global success_key, login_alive
    print('再见{},欢迎再次光临本ATM机'.format(client_list[login_alive]['name']))
    success_key = False
    pass


def check_deposit():
    '''
    查询余额
    :return:
    '''
    global login_alive
    print('您目前的余额为{}'.format(client_list[login_alive]['deposit']))


def add_deposit(money):
    '''
    存款
    :param money: 存款金额
    :return:
    '''
    global login_alive
    client_list[login_alive]['deposit'] = str(int(client_list[login_alive]['deposit']) + int(money))
    print('存款成功！')


def delete_deposit(money):
    '''
    取款
    :param money:取款金额
    :return:
    '''
    global login_alive
    if int(client_list[login_alive]['deposit']) >= int(money):
        client_list[login_alive]['deposit'] = str(int(client_list[login_alive]['deposit']) - int(money))
        print('取款成功！')
    else:
        print('您的余额不足！', end='')
        print('您的余额为{}'.format(client_list[login_alive]['deposit']))


while login_key:
    print('='*30, 'ATM系统', '='*30)
    cardid1 = input('欢迎光临本ATM系统，请输入您的卡号或按q退出:')
    if cardid1.upper() == 'Q':
        break
    else:
        login_in(cardid1)
while success_key:
    print('='*5, '尊敬的{}'.format(format(client_list[login_alive]['name'])), '='*5)
    print('{0:<5}1.存款{1:<10}2.取款{2:<5}'.format(' ', ' ', ' '))
    print('{0:<5}3.查询{1:<10}4.退出{2:<5}'.format(' ', ' ', ' '))
    action_key = input('请输入您想要的操作:')
    if action_key == '1':
        money1 = input('请输入您的存款金额或按q取消操作：')
        if money1.upper() == 'Q':
            continue
        else:
            add_deposit(money1)
    elif action_key == '2':
        money1 = input('请输入您的取款金额或按q取消操作：')
        if money1.upper() == 'Q':
            continue
        else:
            delete_deposit(money1)
    elif action_key == '3':
        check_deposit()
    elif action_key == '4':
        login_out()
    else:
        print('请输入正确的操作指令!')

